<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入会议名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="roomId">
        <el-select v-model="queryParams.roomId" placeholder="请选择会议室">
          <el-option v-for="room in roomList" :key="room.id" :label="room.name" :value="room.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.meeting_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['meeting_info:info:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['meeting_info:info:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['meeting_info:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['meeting_info:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="30" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="会议名称" align="center" prop="name" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="会议室" align="center" prop="roomName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.meeting_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['meeting_info:info:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCancel(scope.row)"
            v-hasPermi="['meeting_info:info:edit']">取消</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleStart(scope.row)"
            v-hasPermi="['meeting_info:info:edit']">开始</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEnd(scope.row)"
            v-hasPermi="['meeting_info:info:edit']">结束</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSummary(scope.row)"
            v-hasPermi="['meeting_info:info:edit']">新增会议纪要</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['meeting_info:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 新增会议纪要弹框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新增会议纪要" prop="summary">
          <file-upload v-model="form.summary" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSummary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改会议列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会议名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入会议名称" :disabled="dis" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <!-- <el-input v-model="form.deptId" placeholder="请输入部门id" /> -->
          <el-select v-model="form.deptId" placeholder="请选择部门" :disabled="dis">
            <el-option v-for="dept in deptList" :key="dept.deptId" :label="dept.deptName" :value="dept.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="紧急程度" prop="urgencyDegree">
          <el-select v-model="form.urgencyDegree" placeholder="请选择紧急程度" clearable :disabled="dis">
            <el-option v-for="dict in dict.type.meeting_degree" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)" /> <!--parseInt有它才能将字典的数字转化为对应的汉字-->
          </el-select>
        </el-form-item>
        <el-form-item label="会议类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择会议类型" clearable :disabled="dis">
            <el-option v-for="dict in dict.type.meeting_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)" /> <!--parseInt有它才能将字典的数字转化为对应的汉字-->
          </el-select>
        </el-form-item>
        <el-form-item label="会议室" prop="roomId">
          <!-- <el-input v-model="form.roomId" placeholder="请输入会议室id" /> -->
          <el-select v-model="form.roomId" placeholder="请选择会议室" :disabled="dis">
            <el-option v-for="room in roomList" :key="room.id" :label="room.name" :value="room.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主持人" prop="compereId">
          <el-select v-model="form.compereId" placeholder="请选择主持人" :disabled="dis">
            <el-option v-for="compere in userList" :key="compere.userId" :label="compere.userName"
              :value="compere.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contactId">
          <el-row gutter={20}>
            <el-col :span="12">
              <el-select v-model="form.contactId" placeholder="请选择会议的联系人">
                <el-option v-for="user in userList" :key="user.userId" :label="user.nickName"
                  :value="user.userId"></el-option>
              </el-select>
            </el-col>
            <el-col :span="12" class="contact-phone-col">
              <label>&nbsp;&nbsp;&nbsp;联系方式</label>
              <span>{{ selectedRelation.phonenumber }}</span>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="与会人" prop="presentId">
          <!-- <el-input v-model="form.deptId" placeholder="请输入部门id" /> -->
          <el-select v-model="form.sharerId" multiple placeholder="请选择与会人" :disabled="dis">
            <el-option v-for="sharer in userList" :key="sharer.userId" :label="sharer.userName" :value="sharer.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分享人" prop="shareId">
          <!-- <el-input v-model="form.deptId" placeholder="请输入部门id" /> -->
          <el-select v-model="form.presentId" multiple placeholder="请选择分享人" :disabled="dis">
            <el-option v-for="present in userList" :key="present.userId" :label="present.userName"
              :value="present.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <!-- <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd hh:MM:ss"
            placeholder="请选择开始时间" :disabled="dis">
          </el-date-picker> -->

          <el-date-picker clearable v-model="form.startTime" type="datetime" value-format="yyyy-MM-dd hh:MM:ss"
            placeholder="请选择开始时间" :disabled="dis">
          </el-date-picker>

        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd hh:MM:ss"
            placeholder="请选择结束时间" :disabled="dis">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="附件" prop="attach">
          <file-upload v-model="form.attach" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" :disabled="dis" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelSummary">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listInfo, getInfo, delInfo, addInfo, updateInfo, updateCancel, updateStart, updateEnd,updateSummary } from "@/api/meeting_info/info";
import { listDept } from "@/api/system/dept";
import { listRoom } from "@/api/meeting_room/room";
import { listUser } from "@/api/system/user";

//import { get } from "sortablejs";

export default {
  name: "Info",
  dicts: ['meeting_type', 'meeting_degree', 'meeting_status'],
  data() {
    return {
      //定义纪要id
      summaryId: null,
      //部门列表
      deptList: {},
      // 定义一个变量
      dis: true,
      // 主持人列表
      userList: {},
      // 会议室管理表格数据
      roomList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dateRange: [],
      // 会议列表表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      title1: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        roomId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  computed: {
    selectedRelation() {
      // 根据选中的 relationId 找到对应的联系人信息
      return this.userList.find(relation => relation.userId === this.form.contactId) || {};
    }
  },
  created() {
    this.getList();
    this.getDeptList();
    this.getRoomList();
    this.getUserList();

  },
  methods: {
    //查询所有人员
    getUserList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询所有会议室
    getRoomList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询所有部门
    getDeptList() {
      listDept().then((result) => {
        this.deptList = result.data;

      });
    },
    /** 查询会议列表列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // getList() {
    //   // 模拟从后端获取数据
    //   // 这里应该是一个 AJAX 请求，例如使用 axios
    //   axios.get('/api/meetings').then(response => {
    //     const data = response.data;
    //     this.infoList = data.records; // 假设后端返回的数据在 records 字段中
    //     this.total = data.total; // 更新总条数
    //     this.loading = false; // 停止遮罩层

    //     // 对 infoList 进行排序
    //     this.sortInfoList();
    //   }).catch(error => {
    //     console.error('获取会议列表失败:', error);
    //     this.loading = false; // 停止遮罩层
    //   });
    // },

    // sortInfoList() {
    //   // 按索引排序，这里假设索引是 id 属性
    //   this.infoList.sort((a, b) => a.id - b.id);
    // },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        deptId: null,
        urgencyDegree: null,
        roomId: null,
        type: null,
        compereId: null,
        contactId: null,
        relationId: null,
        startTime: null,
        endTime: null,
        content: null,
        attach: null,
        remark: null,
        summary: null,
        status: null,
        createUser: null,
        createTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    // 新增会议纪要
    handleSummary(row) {
      const id = row.id;
      this.summaryId=id;
      this.reset();
      this.open1=true;
      this.title1="新增会议纪要";
    },
    // 新增纪要确定按钮
    submitSummary(){
      updateSummary(this.form,this.summaryId).then((result) => {
        //如果新增成功,弹消息
        if (result.code == 200) {
          this.$message.success(result.msg);
          this.getList();
          this.open1=false;
        } else {
          //失败
          this.$message.error(result.msg);
        }
      })
    },
    // 纪要取消按钮
    cancelSummary(){
      this.open1=false;
      this.reset();
    },
    //查看按钮
    handleUpdate(row) {
      this.dis = true;
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看会议";
      });
    },
    // 取消按钮
    handleCancel(row) {
      const id = row.id || this.ids
      updateCancel(id).then((result) => {
        //如果取消成功,弹消息
        if (result.code == 200) {
          this.$message.success(result.msg);
          //刷表格
          this.getList();
        } else {
          //失败
          this.$message.error(result.msg);
        }
      })
    },
    // 开始按钮
    handleStart(row) {
      const id = row.id || this.ids
      updateStart(id).then((result) => {
        //如果开始成功,弹消息
        if (result.code == 200) {
          this.$message.success(result.msg);
          //刷表格
          this.getList();
        } else {
          //失败
          this.$message.error(result.msg);
        }
      })
    },
    // 结束按钮
    handleEnd(row) {
      const id = row.id || this.ids
      updateEnd(id).then((result) => {
        //如果结束成功,弹消息
        if (result.code == 200) {
          this.$message.success(result.msg);
          //刷表格
          this.getList();
        } else {
          //失败
          this.$message.error(result.msg);
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dis=false;
      this.reset();
      this.open = true;
      this.title = "添加会议列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会议列表";
      });
    },
    /** 确定按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会议列表编号为"' + ids + '"的数据项？').then(function () {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('meeting_info/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
